/*
 * Copyright (c) 2007, TopCoder, Inc. All rights reserved.
 */

using System;
using System.Collections.Generic;
using System.Linq;
using Studentium;
using Studentium.Entities;

namespace TopCoder.Util.Calendar.Entry
{
    /// <summary>
    /// <para>
    /// This is a default implementation of the <c>BaseCalendarEntity</c> abstract class.
    /// </para>
    /// <para>
    /// It defines an extra priority field to specify the priority of a calendar entry.
    /// </para>
    /// </summary>
    /// <remarks>
    /// Thread Safety: This class is not thread safe since it's highly mutable.
    /// </remarks>
    ///
    /// <author>snower, fuyun</author>
    /// <version>1.0</version>
    /// <copyright>Copyright (c) 2007, TopCoder, Inc. All rights reserved.</copyright>    
    [Serializable]
    public class CalendarEntry : BaseCalendarEntry
    {
        /// <summary>
        /// <para>Represents the priority of this calendar entry.</para>
        /// <para>
        /// It defaults to <c>Priority.Medium</c> and can be accessed/modified via the namesake property.
        /// </para>
        /// </summary>
        private Priority priority = Priority.Medium;

        // this is purely UI field
        public virtual string DescriptionAndDate
        {
            get
            {
                if (Level == StudentLevel.Unknown)
                    return Description + " " + Date.ToShortTimeString();

                return Level + " " + Description + " " + Date.ToShortTimeString();                    
            }
        }

        // this is purely UI field
        public virtual string FullName
        {
            get
            {
                if (Level == StudentLevel.Unknown)
                    return Description;
                return Level + " " + Description;
            }
        }

        /// <summary>
        /// Represents the property for the priority of this calendar entry. The default value is
        /// <c>Priority.Mdeium</c>.
        /// </summary>
        /// <value>The priority of this calendar entry.</value>
        /// <exception cref="ArgumentException">
        /// if the value is not a predefined Priority enum when setting this property.
        /// </exception>
        public virtual Priority Priority
        {
            get
            {
                return priority;
            }

            set
            {
                if (!Enum.IsDefined(typeof(Priority), value))
                {
                    throw new ArgumentException(
                        "The value set to Priority is not a predefined Priority enum.", "priority");
                }
                priority = value;
            }
        }



        // this is purely UI-validation field
        public virtual int MaximumAttendants
        {
            get
            {
                if (IsPaidClass())
                    return 6;
                
                if ( Description.ToUpper().Contains("DRAMA") )
                    return 10;

                if (Description.ToUpper().Contains("MOVIE"))
                    return 20;

                return 10;
            }
        }

        public virtual StudentLevel Level { get; set; }

        public override int GetHashCode()
        {
            return EntryId;
        }

        public override bool Equals(object obj)
        {
            if (obj == null || !(obj is CalendarEntry))
                return false;

            return ((CalendarEntry) obj).EntryId == EntryId;
        }

        public bool IsPaidClass()
        {
            var freeClasses = new List<string>() { "DRAMA", "MOVIE" };
            return !freeClasses.Any(freeClass => Description.ToUpper().Contains(freeClass.ToUpper()));
        }
    }
}